<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function search() {
            
            var obj=new URLSearchParams(location.search)
            var $lid=obj.get("lid");
            //1.创建xhr异步对象
            var xhr = new XMLHttpRequest();
            //2.创建请求,打开连接
            xhr.open("get", `/laptop/v1/laptop_search/${$lid}`, true)
            //3.发送请求
            xhr.send();
            //4.接收响应
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var r = xhr.responseText;
                    console.log(r)
                    if(r==0){
                        alert("查询失败")
                    }else{
                        var arr = JSON.parse(r);
                        // slid.value=arr[0].lid
                        family_id.value=arr[0].family_id;
                        title.value=arr[0].title;
                        price.value=arr[0].price;
                        promise.value=arr[0].promise;
                        spec.value=arr[0].spec;
                        category.value=arr[0].category;
                        details.value=arr[0].details;
                        shelf_time.value=arr[0].shelf_time;
                        sold_count.value=arr[0].sold_count;
                        // is_onsale.value=arr[0].is_onsale;
                        if(arr[0].is_onsale=1){
                            sale.checked;
                        }else{
                            nsale.checked;
                        }
                        size.value=arr[0].size;
                        Fabric.value=arr[0].Fabric;
                        season.value=arr[0].season;
                        vtype.value=arr[0].vtype;
                    }  
                }
            }
        }
        function revise() {
            // var $lid = slid.value;
            var obj=new URLSearchParams(location.search);
            var $lid=obj.get("lid");
            var $family_id = family_id.value;
            var $title = title.value;
            var $price = price.value;
            var $promise = promise.value;
            var $spec = spec.value;
            var $category = category.value;
            var $details = details.value;
            var $shelf_time = shelf_time.value;
            var $sold_count = sold_count.value;
            var $is_onsale = -1;
            if (sale.checked) {
                $is_onsale = 1
            } else if (nsale.checked) {
                $is_onsale = 0
            } else {
                $is_onsale = -1
            }
            var $size = size.value;
            var $Fabric = Fabric.value;
            var $season = season.value;
            var $vtype = vtype.value;
            
            //1.创建xhr异步对象
            var xhr = new XMLHttpRequest();
            //2.创建请求,打开连接
            xhr.open("put", `/laptop/v1/laptop_revise`, true)
            //3.发送请求
            //3.1创建请求主体
            var formdata = `lid=${$lid}&family_id=${$family_id}&title=${$title}&price=${$price}&promise=${$promise}&spec=${$spec}&category=${$category}&details=${$details}&shelf_time=${$shelf_time}&sold_count=${$sold_count}&is_onsale=${$is_onsale}&size=${$size}&Fabric=${$Fabric}&season=${$season}&vtype=${$vtype}`
            //3.2设置请求头信息
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(formdata)
            //4接收响应
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var r = xhr.responseText;
                    if (r == 1) {
                        alert("修改成功")
                    } else {
                        alert("修改失败")
                    }
                }
            }
        }
    </script>
</head>

<body onload="search()">
    <!-- <input type="text" id="lid">编号<br> -->
    <!-- <button onclick="search()">查询</button><br> -->
    <!-- <input type="text" id="slid">编号<br> -->
    <input type="text" id="family_id">所属家族编号<br>
    <input type="text" id="title">主标题<br>
    <input type="text" id="price">价格<br>
    <input type="text" id="promise">服务承诺<br>
    <input type="text" id="spec">规格/颜色<br>
    <input type="text" id="category">所属分类<br>
    <input type="text" id="details">产品详细说明<br>
    <input type="text" id="shelf_time">上架时间<br>
    <input type="text" id="sold_count">已售出的数量<br>
    <input type="radio" id="sale" name="is_onsale" checked>促销中<br>
    <input type="radio" id="nsale" name="is_onsale">不促销<br>
    <input type="text" id="size">尺寸<br>
    <input type="text" id="Fabric">面料<br>
    <input type="text" id="season">适用季节<br>
    <input type="text" id="vtype">版型<br>
    <button onclick="revise()">修改</button>
    <div id="d1"></div>
</body>
</html>